(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.account.listController', listController);
	listController.$inject = [ '$scope', '$q', 'Constants', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService',
			'accountService', 'commonService'];
	function listController($scope, $q, Constants, gridService, modalService, layoutConfig, validatorConfig, authService, accountService, commonService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("accountList");
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.isShowAcctCcyList = function(){
			if(ctrl.account.custAcctFlag == 'Y'){
				return false;
			}else{
				return true;
			}
		};
		
		ctrl.viewPatternListLookup = function (){
			modalService.openCustomDialogs({
				'dialogsModule' : 'cda.acctPatternLookup'
			});
		};
		
		ctrl.setMoreData = function(resultList){
			var firstLine = "_PARAMS_=";
			var nameValuePairs = "";
			var endOfLine = "\r\n";
			angular.forEach(resultList, function(result, key){
				var pValue = result.parmValue;
				if(key != 0) {
					firstLine += ";";
					nameValuePairs += endOfLine;
				}
				firstLine += result.parmName;		
				nameValuePairs += (result.parmName + '=' + pValue);
			});
			ctrl.account.moreData = firstLine + endOfLine + nameValuePairs;
		};
		
		ctrl.init = function() {
			ctrl.setupDataStatusOptions = commonService.setupDataStatusOptions();
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			ctrl.search();
		};

		ctrl.view = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.account.view', ctrl.selectedRows[0]);
		};

		ctrl.create = function() {
			$scope.go('cda.account.create');
		};

		ctrl.update = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.account.update', ctrl.selectedRows[0]);
		};

		ctrl.delete = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.account.delete', ctrl.selectedRows[0]);
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(function(response) {
				return $q(function(resolved, rejected) {
					resolved(response);
				});
			}, angular.noop).then(function(response){
				accountService.initOptionForSearch().then(function(result) {
					ctrl.custAcctFlagOptions = result.yesNoOptions ? result.yesNoOptions : [];
					ctrl.systemFlagOptions = result.yesNoOptions ? angular.copy(result.yesNoOptions) : [];
					ctrl.acctTypeOptions = result.acctTypeOptions ? result.acctTypeOptions : [];
					ctrl.dataStatusOptions = result.dataStatusOptions ? result.dataStatusOptions : [];
					angular.merge(ctrl.searchCriteria, response.data);
					gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
				},angular.noop);
			}, angular.noop);
			
		};
		
		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);
